package com.yunchen.BooksManagementSystem.manage.servlet;

import com.alibaba.fastjson.JSON;
import com.yunchen.BooksManagementSystem.manage.dao.adminDaoImpl;
import com.yunchen.BooksManagementSystem.user.entity.Books;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "UpdateBookServlet", value = "/UpdateBookServlet")
public class UpdateBookServlet extends HttpServlet {
    private adminDaoImpl ad=new adminDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
       BufferedReader br=request.getReader();
        //String params= br.readLine();
        StringBuilder builder=new StringBuilder();
        String line=null;
        while ((line=br.readLine())!=null){
            builder.append(line);
        }
        System.out.println(builder);
        Books book= JSON.parseObject(String.valueOf(builder), Books.class);
        String name=book.getBookName();
        System.out.println(name);
        int amount=book.getAmount();
        System.out.println(amount);
        int count;
        try {
            count=ad.updateBook(name,amount);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(count);
        response.setContentType("text/json;charset=UTF-8");
        //响应成功标识
        response.getWriter().write("修改成功");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
